package cs276.pe1.spell;

import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import cs276.util.Constants;
import cs276.util.IOUtils;
import cs276.util.StringUtils;

/**
 * This class discovers the maximum number of possible corrections.
 * @author alecmgo@gmail.com
 *
 */
public class MaxPossibleCorrections {

  public static void main(String[] args) throws Exception {
    HashSet<String> wordSet = new HashSet<String>();
    File path = new File(Constants.PE1_DIR + "big.txt.gz");
    for (String line : IOUtils.readLines(IOUtils.openFile(path))) {
      for (String word : StringUtils.tokenize(line)) {
        wordSet.add(word);
      }
    }

    File wordsFile = new File(Constants.PE1_DIR + "spelltest.txt");

    Map<String,List<String>> words = new LinkedHashMap<String,List<String>>();

    for (String line : IOUtils.readLines(IOUtils.openFile(wordsFile))) {
      List<String> split = Arrays.asList(line.split("\\s+"));
      words.put(split.get(0), split.subList(1, split.size()));
    }

    int wordsFound = 0;
    Set<String> wordsNotFoundSet = new HashSet<String>();
    List<String> wordsNotFoundList = new ArrayList<String>();
    for (Map.Entry<String, List<String>> entry : words.entrySet()) {
      final String word = entry.getKey();
      for (String misspelling : entry.getValue()) {
        if(wordSet.contains(word)) {
          wordsFound++;
        } else {
          wordsNotFoundSet.add(word);
          wordsNotFoundList.add(word);
        }
      }
    }
    
    System.out.println("Total words found: " + wordsFound);
    System.out.println("Total words not found: " + wordsNotFoundSet.size());
    System.out.println(" - " + wordsNotFoundSet);
    System.out.println("Total words not found instances: " + wordsNotFoundList.size());
    System.out.println(" - " + wordsNotFoundList);
  }
}
